"""
数据库模型
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, JSON, ForeignKey
from config.database import Base
from sqlalchemy.orm import relationship
from datetime import datetime




class UserProfile(Base):
    """用户档案表"""
    __tablename__ = "user_profiles"
    
    id = Column(Integer, primary_key=True, index=True)
    platform = Column(String(50), nullable=False, index=True)  # linkedin, facebook等
    profile_id = Column(String(100), nullable=False, index=True)  # 平台上的用户ID
    name = Column(String(100), nullable=True)  # 用户名/昵称
    title = Column(String(200), nullable=True)  # 职位
    headline = Column(Text, nullable=True)  # 个人简介/标题
    profile_url = Column(String(500), nullable=True)  # 个人主页链接
    avatar_url = Column(String(500), nullable=True)  # 头像链接
    company = Column(String(200), nullable=True)  # 公司
    industry = Column(String(100), nullable=True)  # 行业
    experience = Column(JSON, nullable=True)  # 工作经历
    education = Column(JSON, nullable=True)  # 教育背景
    skills = Column(JSON, nullable=True)  # 技能
    summary = Column(Text, nullable=True)  # 个人总结
    phone = Column(JSON, nullable=True)  # 联系信息
    website = Column(Text, nullable=True)  # 网站
    address = Column(Text, nullable=True)  # 地址
    email = Column(String(100), nullable=True)  # 邮箱
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    account = Column(String(100), nullable=True, index=True)  # 账号名称
    is_add_friend = Column(Integer, nullable=False, default=False, comment="是否添加为好友")

class Task(Base):
    """任务表"""
    __tablename__ = "tasks"

    id = Column(Integer, primary_key=True, autoincrement=True, index=True, comment="自增主键")
    task_id = Column(String(100), nullable=False, unique=True, index=True, comment="任务ID")
    topic = Column(JSON, nullable=False, comment="话题关键词")
    status = Column(String(20), nullable=False, default="running", comment="任务状态")  
    steps = Column(JSON, nullable=True, comment="步骤信息，JSON格式")
    results = Column(JSON, nullable=True, comment="结果信息，JSON格式")
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")
 
   


class SystemLog(Base):
    """系统日志表"""
    __tablename__ = "system_logs"
    
    id = Column(Integer, primary_key=True, index=True)
    level = Column(String(20), nullable=False, index=True)  # INFO, WARNING, ERROR
    show_in_ui = Column(Integer,  index=True)
    message = Column(Text, nullable=False)
    task_id = Column(String(100), nullable=True, index=True)
    extra_data = Column(JSON, nullable=True)  # 额外数据
    created_at = Column(DateTime, default=datetime.utcnow, index=True)

class Comment(Base):
    """存储从各平台抓取的评论数据"""
    __tablename__ = "comments"

    id = Column(Integer, primary_key=True, autoincrement=True)
    platform = Column(String(50), nullable=False, index=True, comment="评论来源平台")
    link = Column(String(500), nullable=False, comment="原始链接")
    summary = Column(Text, nullable=True, comment="内容摘要")
    comment_name = Column(String(255), nullable=False, comment="评论者名称")
    comment_profile_url = Column(String(500), nullable=True, comment="评论者资料链接")
    comment_time = Column(Text, nullable=True, index=True, comment="评论时间")
    comment_text = Column(Text, nullable=False, comment="评论内容")
    created_at = Column(DateTime, default=datetime.utcnow, index=True, comment="记录创建时间")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="记录更新时间")


class Message(Base):
    """消息表"""
    __tablename__ = "messages"

    id = Column(Integer, primary_key=True, autoincrement=True, index=True, comment="自增主键")
    sender = Column(String(255), nullable=False, comment="发送人")
    receiver = Column(String(255), nullable=False, comment="接收人")
    content = Column(Text, nullable=False, comment="发送的内容")
    sent_at = Column(DateTime, nullable=False, comment="发送的时间")
    created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间")
    is_send = Column(Integer, default=0, comment="是否发送成功")


class GroupInfo(Base):
    """小组信息表"""
    __tablename__ = "group_info"
    
    id = Column(Integer, primary_key=True, autoincrement=True, index=True, comment="自增主键")
    group_name = Column(String(255), nullable=False, comment="组名")
    group_url = Column(String(500), nullable=False, comment="组链接")
    member_count = Column(Integer, default=0, comment="成员数")
    daily_post_count = Column(Integer, default=0, comment="每天发布的帖子量")
    is_joined = Column(Integer, default=0, comment="是否已加入，0-未加入，1-已加入")
    created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")